package com.yfw.maple_leaf_store.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfw.maple_leaf_store.common.BaseContext;
import com.yfw.maple_leaf_store.common.R;
import com.yfw.maple_leaf_store.domain.ShoppingCart;
import com.yfw.maple_leaf_store.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author: yfw
 * @date: 2022/6/18 9:32
 * com.yfw.maple_leaf_store.controller
 */
@RestController
@Slf4j
@RequestMapping("/shopping")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 根据用户id获取购物车信息
     *
     * @return
     */
    @GetMapping("/list")
    public R<Page<ShoppingCart>> list(Integer current, Integer size) {
//        Long currentId = BaseContext.getCurrentId();
        Long currentId = Long.valueOf("1538364171261296641");//测试
        Page<ShoppingCart> pageInfo = new Page<>(current, size);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUid, currentId);
        Page<ShoppingCart> page = shoppingCartService.page(pageInfo, wrapper);
        return R.success(page);
    }

    /**
     * 加入购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
//        Long currentId = Long.valueOf("1538364171261296641");//测试
        shoppingCart.setUid(currentId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUid, currentId).eq(ShoppingCart::getGid, shoppingCart.getGid());//根据用户id

        //查询当前商品是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (cartServiceOne != null) {
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加到购物车，数量默认是1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 删除单列的购物车
     *
     * @param shoppingCart
     * @return
     */
    @DeleteMapping("/delete")
    public R<String> delete(@RequestBody ShoppingCart shoppingCart) {
        Long currentId = BaseContext.getCurrentId();
//        Long currentId = Long.valueOf("1537801628956426242");//测试
        shoppingCart.setUid(currentId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getId, shoppingCart.getId()).eq(ShoppingCart::getUid, shoppingCart.getUid());
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //抽取clean方法到service层
        shoppingCartService.clean();
        return R.success("清空购物车成功");
    }
}
